{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-16T09:33:06.774929Z",
     "start_time": "2025-09-16T09:33:06.763864Z"
    }
   },
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from pyarrow.dataset import parquet_dataset\n",
    "from tqdm import trange,tqdm"
   ],
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "合并client_id表",
   "id": "f0c4f0071c68080"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:33:31.913798Z",
     "start_time": "2025-09-16T09:33:31.898121Z"
    }
   },
   "cell_type": "code",
   "source": [
    "client_split = '../../dataset/MBD-mini/client_split'\n",
    "os.listdir(client_split)"
   ],
   "id": "2370cb72d0cd7ed1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['fold=0', 'fold=1', 'fold=2', 'fold=3', 'fold=4', '_SUCCESS']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:45:46.817758Z",
     "start_time": "2025-09-16T09:45:46.810170Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def merge_df(path):\n",
    "    df_full = pd.DataFrame()\n",
    "    num_folds = len([p for p in os.listdir(path) if p.startswith('fold')])\n",
    "    for i in trange(num_folds):\n",
    "        path_temp = f'{path}/fold={i}'\n",
    "        parquet_path = os.listdir(path_temp)\n",
    "        for p in parquet_path:\n",
    "            full_path = f'{path_temp}/{p}'\n",
    "            df_ = pd.read_parquet(full_path)\n",
    "            df_full = df_full._append(df_)\n",
    "            df_full.reset_index(drop=True, inplace=True)\n",
    "    return df_full"
   ],
   "id": "4780a302f5f5150c",
   "outputs": [],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:45:54.663532Z",
     "start_time": "2025-09-16T09:45:49.002358Z"
    }
   },
   "cell_type": "code",
   "source": "df_client_full = merge_df(client_split)",
   "id": "712bef0a34eba6a1",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5/5 [00:05<00:00,  1.13s/it]\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:45:56.031840Z",
     "start_time": "2025-09-16T09:45:56.021875Z"
    }
   },
   "cell_type": "code",
   "source": "df_client_full.shape",
   "id": "f5738c2a7cb506e5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100224, 1)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:46:29.620269Z",
     "start_time": "2025-09-16T09:46:29.549461Z"
    }
   },
   "cell_type": "code",
   "source": "df_client_full.to_pickle('../data/outdata/client_full.pkl')",
   "id": "7d4294bb9aac73e0",
   "outputs": [],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "合并target表",
   "id": "fbb076dc455780a0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:48:45.949443Z",
     "start_time": "2025-09-16T09:48:45.942059Z"
    }
   },
   "cell_type": "code",
   "source": "targets_path = f'../../dataset/MBD-mini/targets'",
   "id": "3a911badddd1ef2a",
   "outputs": [],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:49:17.289277Z",
     "start_time": "2025-09-16T09:49:06.200581Z"
    }
   },
   "cell_type": "code",
   "source": "df_targets_full = merge_df(targets_path)",
   "id": "1fd10b91cccba303",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5/5 [00:11<00:00,  2.21s/it]\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:49:34.295058Z",
     "start_time": "2025-09-16T09:49:34.089104Z"
    }
   },
   "cell_type": "code",
   "source": "df_targets_full.to_pickle('../data/outdata/targets_full.pkl')",
   "id": "10fffeda6bc0e976",
   "outputs": [],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:56:21.301223Z",
     "start_time": "2025-09-16T09:56:21.289088Z"
    }
   },
   "cell_type": "code",
   "source": "df_targets_full.shape",
   "id": "eafdf52f9463e0e8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1202688, 8)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "合并trx中的所有数据",
   "id": "72d1a58e3dc180f3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:51:07.669176Z",
     "start_time": "2025-09-16T09:51:07.661760Z"
    }
   },
   "cell_type": "code",
   "source": "trx_path = '../../dataset/MBD-mini/ptls/trx'",
   "id": "9b129a2f93b7a9d5",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:51:38.187743Z",
     "start_time": "2025-09-16T09:51:22.926593Z"
    }
   },
   "cell_type": "code",
   "source": "df_trx_full = merge_df(trx_path)",
   "id": "3d95b1b2d1897d7c",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5/5 [00:15<00:00,  3.05s/it]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:52:00.802583Z",
     "start_time": "2025-09-16T09:52:00.792997Z"
    }
   },
   "cell_type": "code",
   "source": "df_trx_full.shape",
   "id": "52cd62489b9f481c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(98721, 14)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T09:57:06.904507Z",
     "start_time": "2025-09-16T09:56:50.866621Z"
    }
   },
   "cell_type": "code",
   "source": "df_trx_full.to_pickle('../data/outdata/trx_full.pkl')",
   "id": "79b9a72879d9c0f6",
   "outputs": [],
   "execution_count": 25
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
